<?php
/**
 * 文章管理
 * @author caoge
 * @version 2012-7-19
 */
class ArticleAction extends AdminBaseAction{
	/**
	 * 文章列表
	 * 
	 * @author caoge
	 * @version 2012-7-22
	 */
	public function index(){
		$nowPage = $this->_get("p","",1);
		$typeId = $this->_post("typeId","strip_tags",-1);
		$where = 'del_flag=0';
		if ($typeId != -1){
			$where .= ' AND type_id='.$typeId;
		}
		$Article = D(Article);
		import("ORG.Util.Page");
		$count = $Article->where($where)->count();
		$p = new Page($count,10);
		
		$list = $Article->relation(true)->where($where)->limit($p->firstRow.','.$p->listRows)
		->order('sort desc,create_time desc')->field('article_id,type_id,title,create_time,is_top,is_recommend,sort,visibility')->select();
		
		$ArticleType = D(ArticleType);
		$typeList = $ArticleType->getTypeList();
		$this->assign('page',$p->show());
		$this->assign('nowPage',$nowPage);
		$this->assign('typeId',$typeId);
		$this->assign('typeList',$typeList);
		$this->assign('list',$list);
		$this->display();
	}
	
	/**
	 * 添加或者编辑文章
	 * 
	 * @author caoge
	 * @version 2012-7-19
	 */
	public function edit(){
		$nowPage = $this->_get('p','',1);
		$articleId = $this->_get('articleId','',0);
		$typeId = $this->_post('typeId','',0);
		$title = $this->_post('title','strip_tags','');
		$content = $_POST['editorValue'];
		$isTop = isset($_POST['isTop'])?1:0;
		$isRecommend = isset($_POST['isRecommend'])?1:0;
		$sort = $this->_post('sort','',0);
		$tagString = $this->_post('aticleTags','','');
		
		if ($title != ''){
			if ($tagString != ''){
				$tagIds = array(); //对应关系表需要的标签ID
				//处理标签，先找到新增的标签，插入到表中
				$tags = explode(',', $tagString);
				$ArticleTag = D(ArticleTag);
				$tagList = $ArticleTag->where('del_flag=0')->select();
				foreach ($tagList as $v){
					foreach ($tags as $key=>$val){
						if(strtolower($val) == strtolower($v['tag_name'])){
							$tagIds[] = $v['tag_id'];
							unset($tags[$key]);
						}
					}
				}
				if (!empty($tags)){
					foreach ($tags as $v){
						$tagIds[] = $ArticleTag->data(array('tag_name'=>$v))->add();
					}
				}
			}
			$data = array(
					'type_id'=>$typeId,
					'title'=>$title,
					'author'=>'caoge',
					'content'=>stripslashes($content),//stripslashes()
					'is_top'=>$isTop,
					'is_recommend'=>$isRecommend,
					'sort'=>$sort
			);
			$Article = D(Article);
			if ($articleId == 0){
				$data['create_time'] = date('Y-m-d H:i:s',time());
				//新增
				$articleId = $Article->data($data)->add();
				if (!empty($tagIds)){
					$ArticleTagRelationship = D(ArticleTagRelationship);
					foreach ($tagIds as $v){
						$ArticleTagRelationship->data(array('article_id'=>$articleId,'tag_id'=>$v))->add();
					}
				}
				$this->success('添加成功',__APP__.'/Article/index/?&p='.$nowPage);
			}else {
				$data['update_time'] = date('Y-m-d H:i:s',time());
				$updateCondition['article_id'] = $articleId;
				$Article->where($updateCondition)->data($data)->save();
				if (!empty($tagIds)){
					$ArticleTagRelationship = D(ArticleTagRelationship);
					$ArticleTagRelationship->where(array('article_id'=>$articleId))->delete();
					foreach ($tagIds as $v){
						$ArticleTagRelationship->data(array('article_id'=>$articleId,'tag_id'=>$v))->add();
					}
				}
				$this->success('编辑成功',__APP__.'/Article/index/?&p='.$nowPage);
			}
		}else {
			$assignedTags = array(); //已有标签默认为空
			if ($articleId == 0){
				$this->assign('actionTile','新文章');
			}else {
				$Article = D(Article);
				$condition['article_id'] = $articleId;
				$articleInfo = $Article->relation(true)->where($condition)->find();
				if (!$articleInfo){
					$this->error('文章不存在',__APP__.'/Article/index/');
				}
				//文章已有的标签
				if (!empty($articleInfo['tag'])){
					foreach ($articleInfo['tag'] as $v){
						$assignedTags[] = $v['tag_name'];
					}
				}
				$this->assign('articleInfo',$articleInfo);
				$this->assign('actionTile','编辑文章');
			}
			$ArticleTag = D(ArticleTag);
			$tagList = $ArticleTag->where('del_flag=0')->select();
			$availableTags = array();
			foreach ($tagList as $v){
				$availableTags[] = $v['tag_name'];
			}
			$ArticleType = D(ArticleType);
			$typeList = $ArticleType->getTypeList();
			$this->assign('nowPage',$nowPage);
			$this->assign('typeList',$typeList);
			$this->assign('assignedTags',json_encode($assignedTags));
			$this->assign('availableTags',json_encode($availableTags));
			$this->display();
		}
	}
	
	/**
	 * 设置文章参数，如浏览权限，分类，置顶，推荐等等
	 * 
	 * @author caoge
	 * @version 2012-8-1
	 */
	public function set(){
		$nowPage = $this->_get('p','',1);
		$articleId = $this->_get('articleId','',0);
		$typeId = $this->_post('typeId','',0);
		$title = $this->_post('title','strip_tags','');
		$isTop = isset($_POST['isTop'])?1:0;
		$isRecommend = isset($_POST['isRecommend'])?1:0;
		$sort = $this->_post('sort','',0);
		$tagString = $this->_post('aticleTags','','');
		if ($title != ''){
			if ($tagString != ''){
				$tagIds = array(); //对应关系表需要的标签ID
				//处理标签，先找到新增的标签，插入到表中
				$tags = explode(',', $tagString);
				$ArticleTag = D(ArticleTag);
				$tagList = $ArticleTag->where('del_flag=0')->select();
				foreach ($tagList as $v){
					foreach ($tags as $key=>$val){
						if(strtolower($val) == strtolower($v['tag_name'])){
							$tagIds[] = $v['tag_id'];
							unset($tags[$key]);
						}
					}
				}
				if (!empty($tags)){
					foreach ($tags as $v){
						$tagIds[] = $ArticleTag->data(array('tag_name'=>$v))->add();
					}
				}
			}
			$data = array(
					'type_id'=>$typeId,
					'title'=>$title,
					'is_top'=>$isTop,
					'is_recommend'=>$isRecommend,
					'sort'=>$sort
			);
			$Article = D(Article);
			$data['update_time'] = date('Y-m-d H:i:s',time());
			$updateCondition['article_id'] = $articleId;
			$Article->where($updateCondition)->data($data)->save();
			if (!empty($tagIds)){
				$ArticleTagRelationship = D(ArticleTagRelationship);
				$ArticleTagRelationship->where(array('article_id'=>$articleId))->delete();
				foreach ($tagIds as $v){
					$ArticleTagRelationship->data(array('article_id'=>$articleId,'tag_id'=>$v))->add();
				}
			}
			$this->success('设置成功',__APP__.'/Article/index/?&p='.$nowPage);
		}else {
			$Article = D(Article);
			$condition['article_id'] = $articleId;
			$articleInfo = $Article->relation(true)->where($condition)->find();
			if (!$articleInfo){
				$this->error('文章不存在',__APP__.'/Article/index/');
			}
			$assignedTags = array(); //已有标签默认为空
			//文章已有的标签
			if (!empty($articleInfo['tag'])){
				foreach ($articleInfo['tag'] as $v){
					$assignedTags[] = $v['tag_name'];
				}
			}
			$this->assign('articleInfo',$articleInfo);
			$this->assign('actionTile','文章设置');
			$ArticleTag = D(ArticleTag);
			$tagList = $ArticleTag->where('del_flag=0')->select();
			$availableTags = array();
			foreach ($tagList as $v){
				$availableTags[] = $v['tag_name'];
			}
			
			$ArticleType = D(ArticleType);
			$typeList = $ArticleType->getTypeList();
			$this->assign('nowPage',$nowPage);
			$this->assign('typeList',$typeList);
			$this->assign('assignedTags',json_encode($assignedTags));
			$this->assign('availableTags',json_encode($availableTags));
			$this->display();
		}
	}
	
	/**
	 * 删除文章
	 * 
	 * @author caoge
	 * @version 2012-7-23
	 */
	public function delete(){
		$nowPage = $this->_get('p','',1);
		$articleId = $this->_get('articleId','',0);
		$Article = D(Article);
		$condition['article_id'] = $articleId;
		$data['del_flag'] = 1;
		$Article->where($condition)->data($data)->save();
		$this->success('删除成功',__APP__.'/Article/index/?&p='.$nowPage);
	}
}